package com.mintcode.ssh.serviceImpl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.mintcode.ssh.beans.User;
import com.mintcode.ssh.dao.BaseDao;
import com.mintcode.ssh.daoimpl.HibernateSessionFactory;
import com.mintcode.ssh.daoimpl.UserDao;
import com.mintcode.ssh.forms.UserForm;
import com.mintcode.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {
	private BaseDao dao;
	private Session session;

	public UserManagerImpl() {
		dao = new UserDao();
	}

	@Override
	public void regUser(UserForm userFrom) {
		session = HibernateSessionFactory.currenSession();
		dao.setSession(session);
		// 获取事务
		Transaction ts = session.beginTransaction();
		// 构造User对象
		User user = new User();
		user.setUserName(userFrom.getUserName());
		user.setPassword(userFrom.getPassword());
		user.setSex(userFrom.getSex());
		// 保存User对象
		dao.saveObject(user);
		// 提交事务
		ts.commit();
		// 关闭Session
		HibernateSessionFactory.closeSession();
	}
}
